package org.szgb.console.sys.dict.service;

import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.szgb.console.sys.dict.entity.SysDictChildren;
import org.szgb.console.sys.dict.mapper.SysDictChildrenMapper;
import org.szgb.console.utils.Page;
import org.szgb.console.utils.UUIDUtil;

import java.util.List;

@Slf4j
@Service
public class SysDictChildrenService {
    private final SysDictChildrenMapper sysDictChildrenMapper;

    @Autowired
    public SysDictChildrenService(SysDictChildrenMapper sysDictChildrenMapper) {
        this.sysDictChildrenMapper = sysDictChildrenMapper;
    }

    @Transactional(readOnly = true)
    public SysDictChildren get(String id) {
        return sysDictChildrenMapper.get(id);
    }

    @Transactional(readOnly = true)
    public List<SysDictChildren> findList(SysDictChildren sysDictChildren) {
        if (sysDictChildren != null && sysDictChildren.getPageDto() != null) {
            Page<SysDictChildren> page = new Page<>(sysDictChildren.getPageDto());
            page.setFirst((page.getPageNo() - 1) * page.getPageSize());
            sysDictChildren.setPage(page);
        }
        return sysDictChildrenMapper.findList(sysDictChildren);
    }

    @Transactional(readOnly = true)
    public Integer getCount(SysDictChildren sysDictChildren) {
        return sysDictChildrenMapper.getCount(sysDictChildren);
    }

    @Transactional()
    public void save(SysDictChildren sysDictChildren) {
        // 检查实体对象的ID是否为null，判断是否为新实体
        if (sysDictChildren.getId() == null || sysDictChildren.getId().isEmpty()) {
            // 对于新实体，设置创建者信息
            sysDictChildren.setCreatedBy(sysDictChildren.getUpdatedBy());
            // 添加UUID
            sysDictChildren.setId(UUIDUtil.getUUID());
            // 执行插入操作
            sysDictChildrenMapper.insert(sysDictChildren);
            // 返回成功插入的信息
        } else {
            // 执行更新操作
            sysDictChildrenMapper.update(sysDictChildren);
        }
    }

    @Transactional
    public void delete(String ids) {
        sysDictChildrenMapper.delete(ids.split(","));
    }
}
